// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import CombatComponent from "../../Combats/CombatComponent";
import GameEvent from "../../Commons/GameEvent";
import { UINames } from "../../Enums/UIEnum";
import { getTimehms } from "../../GlobalFunction";
import GameManager, { GameModel } from "../../Managers/GameManager";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import MapManager, { MapEvent } from "../../Models/Maps/MapManager";
import PetManager, { PetEvent } from "../../Models/Pets/PetManager";
import ShopManager from "../../Models/Shops/ShopManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class CombatUI extends cc.Component {
    @property(cc.Node)
    moveSpeedBuff: cc.Node = null;
    @property(cc.Label)
    moveSpeedTxt: cc.Label = null;
    @property(cc.Node)
    attackBff: cc.Node = null;
    @property(cc.Label)
    attackTxt: cc.Label = null;
    @property([cc.Label])
    attributeTxts: cc.Label[] = [];
    @property(cc.Label)
    chatTxt: cc.Label = null;
    @property(cc.Node)
    gateUI: cc.Node = null;
    @property(cc.Node)
    goldBossUI: cc.Node = null;
    @property(cc.Node)
    chatNode: cc.Node = null;



    // @property(cc.Label)
    // mapNameTxt: cc.Label = null;
    protected onLoad(): void {
        this.moveSpeedBuff.active = this.attackBff.active = false;
        this.goldBossUI.active = false;

    }

    protected onEnable(): void {
        this.drawView();
        this.drawPetInfo();
        SaveManager.single.getChatMessage();
        GameEvent.eventNode.on(MapEvent.SELECTMAP_EVENT, this.onSelectMap, this);
        GameEvent.eventNode.on(PetEvent.PETFIGHT_EVENT, this.onUpdatePet, this);
        GameEvent.eventNode.on(PetEvent.PETTAKEOFF_EVENT, this.onUpdatePet, this);
        GameEvent.eventNode.on(GameEvent.UPDATACHATMESSAGE_EVENT, this.onUpdataMessage, this);
        GameEvent.eventNode.on(GameEvent.STARTFUBEN_EVENT, this.OnStartFuBen, this);
        GameEvent.eventNode.on(GameEvent.ENDFUBEN_EVENT, this.OnEndFuBen, this);


        this.schedule(this.onTime, 1);

    }

    protected onDisable(): void {
        this.unschedule(this.onTime);
        GameEvent.eventNode.off(MapEvent.SELECTMAP_EVENT, this.onSelectMap, this);
        GameEvent.eventNode.off(PetEvent.PETFIGHT_EVENT, this.onUpdatePet, this);
        GameEvent.eventNode.off(PetEvent.PETTAKEOFF_EVENT, this.onUpdatePet, this);
        GameEvent.eventNode.off(GameEvent.UPDATACHATMESSAGE_EVENT, this.onUpdataMessage, this);
        GameEvent.eventNode.off(GameEvent.STARTFUBEN_EVENT, this.OnStartFuBen, this);
        GameEvent.eventNode.off(GameEvent.ENDFUBEN_EVENT, this.OnEndFuBen, this);
    }
    OnStartFuBen() {
        GameManager.single.gameModel = GameModel.FuBenModel;
        this.gateUI.active = false;
        this.goldBossUI.active = true;
        this.chatNode.active = false;

    }
    OnEndFuBen() {
        GameManager.single.gameModel = GameModel.GateModel;
        this.goldBossUI.active = false;
        this.gateUI.active = true;
        this.chatNode.active = true;

    }
    onUpdataMessage() {
        var arr = SaveManager.single.chatMessageList;
        if (arr != null && arr.length > 0) {
            if (arr[0].nickName != null)
                this.chatTxt.string = arr[0].nickName + "：" + arr[0].message;
            else {
                this.chatTxt.string = "？？？：" + arr[0].message;
            }
        } else {
            this.chatTxt.string = "";
        }
    }
    onUpdatePet() {
        this.drawPetInfo();
    }

    drawPetInfo() {

        // this.attributeTxt0.string = "";
        // this.attributeTxt1.string = "";
        // var userdata = SaveManager.single.userdata;
        // var petEy = PetManager.single.getPetEntity(userdata.petWearList[0]);
        // if (petEy != null) {
        //     this.attributeTxt0.string = petEy.attributeDes0;
        //     this.attributeTxt1.string = petEy.attributeDes1;
        // }
    }

    onTime() {
        this.attackBff.active = ShopManager.single.attackTime > 0;
        this.attackTxt.string = getTimehms(ShopManager.single.attackTime);
        this.moveSpeedBuff.active = ShopManager.single.moveSpeedTime > 0;
        this.moveSpeedTxt.string = getTimehms(ShopManager.single.moveSpeedTime);
        SaveManager.single.getChatMessage();
    }
    onSelectMap() {
        this.drawView();
    }

    drawView() {

        var userdata = SaveManager.single.userdata;
        var mapTp = MapManager.single.getMapTemplate(userdata.currentMapId);
        // this.mapNameTxt.string = mapTp.mapName;
    }

    mapClick() {
        UIManager.single.showUI(UINames.MapUI);
    }
    taskClick() {
        UIManager.single.showUI(UINames.TaskUI);
    }
    siginClick() {
        UIManager.single.showUI(UINames.SigninUI);
    }

    monthCardClick() {
        UIManager.single.showUI(UINames.MonthCardUI);
    }
    perpetualCardClick() {
        UIManager.single.showUI(UINames.PerpetualCardUI);
    }

    openChatUiClick() {
        UIManager.single.showUI(UINames.ChatUI);
    }

    fubenClick() {
        UIManager.single.showUI(UINames.FuBenUI);
    }
    giftClick() {
        UIManager.single.showUI(UINames.GiftUI);
    }
    exitFuBenClick() {
        UIManager.single.showDialog("确定退出悬赏挑战吗？", () => {
            CombatComponent.startCombat = false;
            //副本时间到
            GameEvent.eventNode.emit(GameEvent.ENDFUBEN_EVENT);
        }, null, this, true);
    }

    protected update(dt: number): void {

    }
}
